<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="房屋简介" prop="houseName">
        <el-input
          v-model="queryParams.houseName"
          placeholder="请输入房屋简介"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['house:house:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['house:house:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['house:house:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="houseList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="房屋Id" align="center" prop="id" />
      <el-table-column label="小区Id" align="center" prop="plotName" />
      <el-table-column label="房屋简介" align="center" prop="houseName" />
      <el-table-column label="房屋面积(㎡)" align="center" prop="homeArea" />
      <el-table-column label="卧室数量(间)" align="center" prop="bedroom" />
      <el-table-column label="客厅数量(间)" align="center" prop="drawing" />
      <el-table-column label="卫生间数量(间)" align="center" prop="restroom" />
      <el-table-column label="朝向Id" align="center" prop="orientationId">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.orientation"
            :value="scope.row.orientationId"
          />
        </template>
      </el-table-column>
      <el-table-column label="期望售价" align="center" prop="homePrice" />
      <el-table-column label="平方价格" align="center" prop="squarePrice" />
      <el-table-column label="审核状态" align="center" prop="isExamine">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.is_examine"
            :value="scope.row.isExamine"
          />
        </template>
      </el-table-column>
      <el-table-column label="上架状态" align="center" prop="isPutaway">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.is_putaway"
            :value="scope.row.isPutaway"
          />
        </template>
      </el-table-column>
      <!-- <el-table-column label="0整租，1合租" align="center" prop="leaseType" /> -->
      <el-table-column label="省" align="center" prop="name1" />
      <el-table-column label="市" align="center" prop="name2" />
      <el-table-column label="区" align="center" prop="name3" />
      <el-table-column
        label="地址经度"
        align="center"
        prop="addressLongitude"
      />
      <el-table-column label="地址纬度" align="center" prop="addressLatitude" />
      <el-table-column label="具体地址" align="center" prop="address" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['house:house:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['house:house:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改房屋基础对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="小区" prop="plotId">
          <el-select v-model="form.plotId" placeholder="请选择">
            <el-option
              v-for="item in plotList"
              :key="item.plotId"
              :label="item.plotName"
              :value="item.plotId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="房屋简介" prop="houseName">
          <el-input v-model="form.houseName" placeholder="请输入房屋简介" />
        </el-form-item>
        <el-form-item label="房屋面积(㎡)" prop="homeArea">
          <el-input v-model="form.homeArea" placeholder="请输入房屋面积" />
        </el-form-item>
        <el-form-item label="卧室数量(间)" prop="bedroom">
          <el-input v-model="form.bedroom" placeholder="请输入卧室数量" />
        </el-form-item>
        <el-form-item label="客厅数量(间)" prop="drawing">
          <el-input v-model="form.drawing" placeholder="请输入客厅数量" />
        </el-form-item>
        <el-form-item label="卫生间数量(间)" prop="restroom">
          <el-input v-model="form.restroom" placeholder="请输入卫生间数量" />
        </el-form-item>
        <el-form-item label="朝向Id" prop="orientationId">
          <el-select v-model="form.orientationId" placeholder="请选择">
            <el-option
              v-for="item in orientations"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="期望售价" prop="homePrice">
          <el-input v-model="form.homePrice" placeholder="请输入期望售价" />
        </el-form-item>
        <el-form-item label="平方价格" prop="squarePrice">
          <el-input v-model="form.squarePrice" placeholder="请输入平方价格" />
        </el-form-item>
        <el-form-item label="审核状态" prop="examine">
          <el-select v-model="form.isExamine" placeholder="请选择">
            <el-option
              v-for="item in examine"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上架状态" prop="isPutaway">
          <el-select v-model="form.isPutaway" placeholder="请选择">
            <el-option
              v-for="item in putway"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="省" prop="city1">
          <el-select
            v-model="form.city1"
            placeholder="请选择"
            @change="citys2(form.city1)"
          >
            <el-option
              v-for="item in city1"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="市" prop="city2">
          <el-select
            v-model="form.city2"
            placeholder="请选择"
            @change="citys3(form.city2)"
          >
            <el-option
              v-for="item in city2"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="区" prop="city3">
          <el-select v-model="form.city3" placeholder="请选择">
            <el-option
              v-for="item in city3"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址经度" prop="addressLongitude">
          <el-input
            v-model="form.addressLongitude"
            placeholder="请输入地址经度"
          />
        </el-form-item>
        <el-form-item label="地址纬度" prop="addressLatitude">
          <el-input
            v-model="form.addressLatitude"
            placeholder="请输入地址纬度"
          />
        </el-form-item>
        <el-form-item label="具体地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入具体地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listHouse,
  getHouse,
  delHouse,
  addHouse,
  updateHouse,
} from "@/api/house/house";
import { getCity, getPlot, getCityById } from "@/api/house/myhouse";

export default {
  name: "House",
  dicts: ["is_putaway", "is_examine", "orientation"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 房屋基础表格数据
      houseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        houseName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      //省
      city1: [],
      city2: [],
      city3: [],

      //上架状态
      putway: [
        {
          value: 1,
          label: "已上架",
        },
        {
          value: "0",
          label: "未上架",
        },
      ],
      //审核状态
      examine: [
        {
          value: 1,
          label: "审核通过",
        },
        {
          value: 0,
          label: "审核不通过",
        },
      ],
      //朝向状态
      orientations: [
        {
          value: 0,
          label: "朝南",
        },
        {
          value: 1,
          label: "朝北",
        },
        {
          value: 2,
          label: "朝东",
        },

        {
          value: 3,
          label: "朝西",
        },
      ],

      //获取小区
      plotList: [],
    };
  },
  created() {
    this.getList();
    this.citys1();

    this.getPlots();
  },
  methods: {
    //获取小区
    getPlots() {
      getPlot().then((res) => {
        this.plotList = res;
      });
    },
    //获取省
    citys1() {
      getCity(0).then((res) => {
        this.city1 = res;
        // console.log(res);
      });
    },
    //获取市
    citys2(id) {
      this.city2 = [];
      getCity(id).then((res) => {
        this.city2 = res;
        // console.log("2222");

        // console.log(res);
      });
    },
    //获取区
    citys3(id) {
      this.city3 = [];
      getCity(id).then((res) => {
        this.city3 = res;
        // console.log("3333");

        // console.log(res);
      });
    },
    aCity2(id) {
      this.city2 = [];
      getCityById(id).then((res) => {
        this.city2[0] = res;
      });
    },
    aCity3(id) {
      this.city3 = [];
      getCityById(id).then((res) => {
        this.city3[0] = res;
      });
    },
    /** 查询房屋基础列表 */
    getList() {
      this.loading = true;
      listHouse(this.queryParams).then((response) => {
        this.houseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        plotId: null,
        houseName: null,
        homeArea: null,
        bedroom: null,
        drawing: null,
        restroom: null,
        orientationId: null,
        homePrice: null,
        squarePrice: null,
        isExamine: null,
        isPutaway: null,
        createTime: null,
        leaseType: null,
        city1: null,
        city2: null,
        city3: null,
        addressLongitude: null,
        addressLatitude: null,
        address: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加房屋基础";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      this.aCity2(row.city2);
      this.aCity3(row.city3);
      //获取城市
      getHouse(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改房屋基础";
      });
      // console.log(this.form);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateHouse(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHouse(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除房屋基础编号为"' + ids + '"的数据项？')
        .then(function () {
          return delHouse(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "house/house/export",
        {
          ...this.queryParams,
        },
        `house_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
